package com.cxb.service;

import java.util.Random;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.cxb.R;
import com.cxb.manager.CxbEventBus;
import com.cxb.manager.RootContext;
import com.cxb.receiver.ScreenFocusReceiver;
import com.cxb.view.SmallCompassView;

public class ScreenMonitor extends Service implements Callback {

	private WindowManager windowManager;
	private WindowManager.LayoutParams mWMParams;
	private View mScreenView;
	public static boolean isShowing = false;
	public static final String ACTION_SCREEN_MONITOR_SHOW = "ACTION_SCREEN_MONITOR_SHOW";
	public static final String ACTION_SCREEN_MONITOR_DIMISS = "ACTION_SCREEN_MONITOR_DIMISS";
	private ScreenMonitorStateReceiver screenMonitorStateReceiver;
	private Handler mHandler;
	public static long lastFocusTime;
	private PowerManager mPowerManager;
	private BroadcastReceiver screenStateReceiver;
	private SmallCompassView smallCompassView;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {

		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		PowerManager.WakeLock mWakeLock = pm.newWakeLock(
				PowerManager.SCREEN_DIM_WAKE_LOCK, "screen");
		mWakeLock.acquire();

		lastFocusTime = System.currentTimeMillis();
		mHandler = new Handler(this);
		windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		mScreenView = LayoutInflater.from(this).inflate(
				R.layout.screen_monitor, null);
		smallCompassView = (SmallCompassView) mScreenView
				.findViewById(R.id.small_compass_view);
		mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ACTION_SCREEN_MONITOR_SHOW);
		intentFilter.addAction(ACTION_SCREEN_MONITOR_DIMISS);
		screenMonitorStateReceiver = new ScreenMonitorStateReceiver();
		this.registerReceiver(screenMonitorStateReceiver, intentFilter);

		screenStateReceiver = new ScreenFocusReceiver();

		this.registerReceiver(screenStateReceiver, new IntentFilter(
				Intent.ACTION_SCREEN_OFF));

		CxbEventBus.resgist(CxbEventBus.EVENT_GPS_CHANGE, this);
		// mHandler.postDelayed(screenMonitorShowTask, SCREEN_TIME_OUT_TIME);
		//
		// mScreenView.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// dimissScreenView();
		//
		// }
		// });
//		showScreenView();
		// TimerTask timerTask = new TimerTask() {
		//
		// @Override
		// public void run() {
		// if (System.currentTimeMillis() - lastFocusTime >
		// SCREEN_TIME_OUT_TIME) {
		// mHandler.sendEmptyMessage(0);
		// }
		//
		// }
		// };
		// Timer timer = new Timer();
		// timer.schedule(timerTask, 1 * 1000);
		super.onCreate();
	}

	public void onEventMainThread(Integer angle) {
		Log.i("test", "angle=" + angle);
		smallCompassView.rotateCompass(angle);
	}

	@Override
	public void onDestroy() {
		this.unregisterReceiver(screenMonitorStateReceiver);
		this.unregisterReceiver(screenStateReceiver);
		CxbEventBus.unResgist(CxbEventBus.EVENT_GPS_CHANGE, this);
		super.onDestroy();
	}

	public void dimissScreenView() {
		if (isShowing) {
			try {
				windowManager.removeView(mScreenView);
			} catch (Exception e) {
				e.printStackTrace();
			}
			isShowing = false;

		}
	}

	private void showScreenView() {

		if (isShowing) {
			return;
		}

		isShowing = true;
		DisplayMetrics dm = new DisplayMetrics();
		dm = getResources().getDisplayMetrics();

		mWMParams = new WindowManager.LayoutParams();
		mWMParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

		mWMParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_FULLSCREEN;
		mWMParams.flags &= ~40;
		// mWMParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
		mWMParams.width = mWMParams.FILL_PARENT;
		mWMParams.height = mWMParams.FILL_PARENT;

		windowManager.addView(mScreenView, mWMParams);

		mScreenView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// smallCompassView.rotateCompass(new Random().nextInt(365));
				dimissScreenView();
			}
		});
	}

	class ScreenMonitorStateReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(ACTION_SCREEN_MONITOR_DIMISS)) {
				dimissScreenView();
			} else if (intent.getAction().equals(ACTION_SCREEN_MONITOR_SHOW)) {
				showScreenView();
				if (mPowerManager.isScreenOn()) {
					RootContext.getInstance().runCommand(
							"input keyevent 85");
				}
			}

		}
	}

	@Override
	public boolean handleMessage(Message arg0) {
		showScreenView();
		return false;
	}
}
